<?php
/**
 * Smart Clinic
 *
 ** LICENSE
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 *
 * @copyright   2012 Greensun
 * @author      Hoang Minh
 * @version     1.0
 * @since       2012/08/30
 */

/**
 * Customer Management Controller
 * @copyright   2012 Greensun
 * @author      Hoang Minh
 * @version     1.0
 * @since       2012/08/30
 */
class MSubCategoryController extends AppController  {
	public $helpers = array('Html', 'Form');
	public $uses = array('Category', 'Subcategory', 'Viewsubcategory');
	/**
	 *  Action load page default layout
	 */
	public function index() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Quản lí nhóm con');		
		$this -> set('type', 'subcategory');
		if(isset($_GET['id'])) {
			$subCategoryList = $this -> Viewsubcategory -> find('all', array('conditions' => array('cateid' => $_GET['id']), 'order' => array('subcatetoporder' => 'ASC')));
			$category = $this -> Category -> find('first', array('conditions' =>array('id' => $_GET['id'])));
			$this -> set('categoryId', $category['Category']['id']);
			$this -> set('title_for_layout', 'Nhóm ' . $category['Category']['name'] . ' - Quản lí nhóm con');
		} else {
			$subCategoryList = $this -> Viewsubcategory -> find('all', array('order' => array('catetoporder' => 'ASC', 'subcatetoporder' => 'ASC')));
			$this -> set('title_for_layout', 'Quản lí nhóm con');
			$this -> set('categoryId', null);
		}
		$this -> set('subCategoryList', $subCategoryList);		
		
		$this -> set('message', $_SESSION['message'] == null ? '' : $_SESSION['message']);
		$_SESSION['message'] = null;
	}
	
	public function insert() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Thêm nhóm con mới');		
		$this -> set('type', 'subcategory');
		$this -> set('message', '');
		if($_GET['id'] != null) {
			$this -> set('categoryId', $_GET['id']);
			$category = $this -> Category -> find('first', array('conditions' =>array('id' => $_GET['id'])));
			$this -> set('title_for_layout', 'Nhóm ' . $category['Category']['name'] . ' - Thêm nhóm con mới');
		} else {
			$this -> set('categoryId', null);
			$this -> set('title_for_layout', 'Thêm nhóm con mới');
			
			$categoryList = $this -> Category -> find('all', array('order' => array('toporder' => 'ASC')));
			$this -> set('categoryList', $categoryList);
		}
		if ($this -> request -> is('post')) {
			$arrData = $_POST['data'];
			$lastSubcategory = $this -> Subcategory -> find('first', array('conditions' => array('categoryid' => $arrData['Subcategory']['categoryid']), 'order' => array('toporder' => 'DESC')));
			if($lastSubcategory['Subcategory']['toporder'] == null) {
				$lastSubcategory['Subcategory']['toporder'] = 0;
			}
			$arrData['Subcategory']['status'] = '1';
			$arrData['Subcategory']['toporder'] = $lastSubcategory['Subcategory']['toporder'] + 1;
			if($this -> Subcategory -> save($arrData)) {
				$_SESSION['message'] = "Thêm nhóm con thành công";
			} else {
				$_SESSION['message'] = "Thêm nhóm con thất bại";
			}
			if($_POST['cateflg'] == '1') {
				$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index?id='.$arrData['Subcategory']['categoryid']));
			} else {
				$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index'));
			}
		}
	}
	
	public function update() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Sửa thông tin nhóm con');
		$this -> set('title_for_layout', 'Sửa thông tin nhóm con');
		$this -> set('type', 'category');
		$this -> set('message', '');
		
		if ($this -> request -> is('post')) {
			$arrData = $_POST['data'];
			if($this -> Category -> save($arrData)) {
				$_SESSION['message'] = "Sửa thông tin nhóm con thành công";
			} else {
				$_SESSION['message'] = "Sửa thông tin nhóm con thất bại";
			}
			if($_POST['cateflg'] == '1') {
				$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index?id='.$arrData['Subcategory']['categoryid']));
			} else {
				$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index'));
			}
		} else {
			if($_GET['categoryid'] != null) {
				$this -> set('categoryId', $_GET['categoryid']);
				$category = $this -> Category -> find('first', array('conditions' =>array('id' => $_GET['categoryid'])));
				$this -> set('title_for_layout', 'Nhóm ' . $category['Category']['name'] . ' - Sửa thông tin nhóm con');
			} else {
				$this -> set('categoryId', null);
				$this -> set('title_for_layout', 'Sửa thông tin nhóm con');			
			}
			$categoryList = $this -> Category -> find('all', array('order' => array('toporder' => 'ASC')));
			$this -> set('categoryList', $categoryList);
			$updatedItem = $this -> Subcategory -> find('first', array('conditions' => array('id' => $_GET['id'])));
			$this -> set('updatedItem', $updatedItem);
		}
	}
	
	public function delete() {
		if($this -> Subcategory -> delete(array('id' => $_GET['id']))) {
			$_SESSION['message'] = "Xóa nhóm con thành công";
		} else {
			$_SESSION['message'] = "Xóa nhóm con thất bại";
		}
		if($_POST['cateflg'] == '1') {
			$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index?id='.$arrData['Subcategory']['categoryid']));
		} else {
			$this -> redirect(array('controller'=>'msubcategory', 'action' => 'index'));
		}
	}
	
	public function changeorder() {
		$data = $_POST['data'];
		
		foreach($data as $eachSubcategory) {
			$this->Subcategory->id = $eachSubcategory['id'];
    		$this->Subcategory->saveField('toporder', intval($eachSubcategory['index']));
		}
		echo json_encode($_POST['categoryid']);
		exit();
	}
}